package TeachLesson;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "fileUploadServlet")
public class fileUploadServlet extends HttpServlet {
    // 上传配置
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String workerNo=request.getParameter("workerNo");
        String courseNo=request.getParameter("courseNo");
        String dirName=request.getParameter("dirName");
        response.setCharacterEncoding("GBK");
        request.setCharacterEncoding("GBK");
        response.getWriter().println(workerNo);
        //指定文件存放位置
        String filePath="F:\\JWGLXT\\web\\StoreResoures\\"+workerNo+"\\"+courseNo+"\\"+dirName;
        //创建smartup对象
        SmartUpload su=new SmartUpload();
        response.setCharacterEncoding("GBK");
        //初始化
        su.initialize(getServletConfig(),request,response);
        //设置上传文件最大值
        su.setMaxFileSize(1024*1024*10);
        try {
            //上传文件
            su.upload();
            //文件保存
            int r=su.save(filePath);
            response.getWriter().println(filePath+"上传成功"+r+"个文件");
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
